﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class DBheper
    {
        public static string str = "server=.;pwd=123;uid=sa;database=Userinfo;";
        public static DataTable Query(string sql,params SqlParameter[] sqlParameters) 
        {
            using (SqlDataAdapter adapter = new SqlDataAdapter(sql, str))
            {
                if (sqlParameters != null && sqlParameters.Count() > 0)
                    adapter.SelectCommand.Parameters.AddRange(sqlParameters);
                using(DataTable table=new DataTable())
                {
                    adapter.Fill(table);
                    return table;
                }
            }
            //SqlDataAdapter adapter = new SqlDataAdapter(sql, str);
            //DataTable table = new DataTable();
            //adapter.Fill(table);
            //return table;
        }

        //非查询  using     close
        public static int NonQuery(string sql)
        {
            using (SqlConnection sqlConnection = new SqlConnection(str))
            {
                using (SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection))
                {
                    sqlConnection.Open();
                    return sqlCommand.ExecuteNonQuery();
                }
            }
        }
    }
}
